package com.apex.shared.starter.core;

import com.apex.shared.starter.constant.HttpStatusCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * 统一返回结果类
 *
 * @author MoNaiHui
 */
@Getter
@Setter
@Accessors(chain = true)
public class Result<T> {

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 提示消息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 是否成功
     */
    private Boolean success;

    // 私有构造函数
    private Result() {
    }

    /**
     * 成功
     */
    public static <T> Result<T> success() {
        return success(null);
    }

    /**
     * 成功，带数据
     */
    public static <T> Result<T> success(T data) {
        return new Result<T>()
                .setCode(HttpStatusCode.OK.getValue())
                .setMessage(HttpStatusCode.OK.getReasonPhrase())
                .setData(data)
                .setSuccess(true);
    }

    /**
     * 成功，自定义消息
     */
    public static <T> Result<T> success(String message) {
        return new Result<T>()
                .setCode(HttpStatusCode.OK.getValue())
                .setMessage(message)
                .setSuccess(true);
    }

    /**
     * 成功，自定义消息和数据
     */
    public static <T> Result<T> success(String message, T data) {
        return new Result<T>()
                .setCode(HttpStatusCode.OK.getValue())
                .setMessage(message)
                .setData(data)
                .setSuccess(true);
    }

    /**
     * 失败，使用 HttpStatusCode
     */
    public static <T> Result<T> fail(HttpStatusCode httpStatusCode) {
        return new Result<T>()
                .setCode(httpStatusCode.getValue())
                .setMessage(httpStatusCode.getReasonPhrase())
                .setSuccess(false);
    }

    /**
     * 失败，使用 HttpStatusCode 和消息
     */
    public static <T> Result<T> fail(HttpStatusCode httpStatusCode, String message) {
        return new Result<T>()
                .setCode(httpStatusCode.getValue())
                .setMessage(message)
                .setSuccess(false);
    }

    /**
     * 失败，自定义错误码和消息
     */
    public static <T> Result<T> fail(Integer code, String message) {
        return new Result<T>()
                .setCode(code)
                .setMessage(message)
                .setSuccess(false);
    }

    /**
     * 失败，自定义消息
     */
    public static <T> Result<T> fail(String message) {
        return fail(HttpStatusCode.INTERNAL_SERVER_ERROR, message);
    }

    /**
     * 判断是否成功
     */
    public boolean isSuccess() {
        return this.success != null && this.success;
    }
} 